import { createVNode, render } from 'vue'
import ZyMessage from './ZyMessage.vue'

// 创建元素用来挂载虚拟 DOM
const divVNode = createVNode('div', { class: 'message' })
render(divVNode, document.body)

const divEl = divVNode.el

let timer = null
export const message = (message, type) => {
  // 如果消息已经存在
  if (timer) return
  // 创建虚拟 DOM
  const comVNode = createVNode(ZyMessage, { message, type })
  render(comVNode, divEl)

  timer = setTimeout(() => {
    render(null, divEl)
    clearTimeout(timer)
    timer = null
  }, 2000)
}
